package com.example.myaccount.adapter;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myaccount.R;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.bean.DailyStatistics;
import com.example.myaccount.bean.Flow;

import java.util.ArrayList;

public class RvFlowAdapter extends RecyclerView.Adapter<RvFlowAdapter.ViewHolder>{

    public ArrayList<ArrayList<Flow>> totalFlowList;

    public ArrayList<DailyStatistics> dialyStatisticsList;

    public RvFlowAdapter(ArrayList<ArrayList<Flow>> totalFlowList, ArrayList<DailyStatistics> dialyStatisticsList) {
        this.totalFlowList = totalFlowList;
        this.dialyStatisticsList = dialyStatisticsList;
    }

    public ArrayList<ArrayList<Flow>> getTotalFlowList() {
        return totalFlowList;
    }

    public void setTotalFlowList(ArrayList<ArrayList<Flow>> totalFlowList) {
        this.totalFlowList = totalFlowList;
    }

    public ArrayList<DailyStatistics> getDialyStatisticsList() {
        return dialyStatisticsList;
    }

    public void setDialyStatisticsList(ArrayList<DailyStatistics> dialyStatisticsList) {
        this.dialyStatisticsList = dialyStatisticsList;
    }

    @NonNull
    @Override
    public RvFlowAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_daily_flow, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mTvDate.setText(dialyStatisticsList.get(position).getDate());
        holder.mTvDailyIncome.setText(new Double(dialyStatisticsList.get(position).getDailyIncome()).toString());
        holder.mTvDailyOutcome.setText(new Double(dialyStatisticsList.get(position).getDailyOutcome()).toString());
        holder.mRvDailyFlow.setLayoutManager(new SyLinearLayoutManager(MainActivity.getContext(),LinearLayoutManager.VERTICAL,false));
        holder.mRvDailyFlow.setAdapter(new RvDailyFlowAdapter(totalFlowList.get(position)));
    }

    @Override
    public int getItemCount() {
        return totalFlowList == null ? 0 : totalFlowList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout mLlContainer;
        TextView mTvDate, mTvDailyIncome, mTvDailyOutcome;
        RecyclerView mRvDailyFlow;

        public ViewHolder(View itemView) {
            super(itemView);
            mLlContainer = itemView.findViewById(R.id.ll_container);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mTvDailyIncome = itemView.findViewById(R.id.tv_daily_income);
            mTvDailyOutcome = itemView.findViewById(R.id.tv_daily_outcome);
            mRvDailyFlow = itemView.findViewById(R.id.rv_daily_flow);
        }
    }
}
